/**
 * 
 */
package info.niwota.profile;

import info.niwota.profile.web.Helper;
import info.niwota.webapp.WebIntents;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author qiangli
 * 
 */
public class ViewProfileActivity extends BaseProfileActivity {

	private static final String TAG = "ViewProfileActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_LEFT_ICON);

		setContentView(R.layout.profile_view);

		getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.icon);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
	}
	
	private String getString(Bundle data, String key) {
		return (data == null ? null : data.getString(key));
	}

	@Override
	protected void onResume() {
		Intent data = getIntent();
		setProfileId(data);
		//
		super.onResume();
	}
	
	private void setProfileId(Intent data) {
		Bundle extras = data.getExtras();

		peer = getString(extras, WebIntents.EXTRA_PEER);
		
		if (Debug.DEBUG) {
			Log.d(TAG, "onCreate peer: " + peer);
		}
		
		TextView id = (TextView) findViewById(R.id.profile_id);
		id.setText(peer);

		boolean star = Preferences.isPeerStarred(this, peer);
		ImageView btn = (ImageView) findViewById(R.id.profile_btn_fav);
		btn.setImageResource(star ? R.drawable.favorite48: R.drawable.favorite48_grey);
		btn.setOnClickListener(this);
		
		if (Debug.DEBUG) {
			Log.d(TAG, " star: " + star +" peer: " + peer);
		}
		
		setTitle(peer);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.profile_view_option, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case R.id.profile_delete_opt_menu_id:
			doDeletePeer();
			break;
		case R.id.profile_refresh_opt_menu_id:
			loadProfile(true);
			break;
		}
		return true;
	}

	private void doDeletePeer() {
		Helper.deletePeer(this, peer);
		finish();
	}
}
